/**
 * 
 */
package com.gosophia.commons;


/**
 * 功能：      排序工具类
 * @author zhangdongsheng
 * @since  2010-8-6
 *  Copyright (c) 2010 Gosophia corporation All Rights Reserved
 */
public class OrderCondition {
    public static final String ASC = "asc";
    public static final String DESC = "desc";
    
    private boolean isASC;
    private String propertyName;
    
    /**
     * 排序列 可以是 几个列运算的结果，
     * 但要求 属性前加“_e.”，如 order by (orgId - version)，
     * 写成new OrderCondition("${orgId} - ${version}", true/false);
     * @param propertyName
     * @param isASC
     */
    public OrderCondition(String propertyName,boolean isASC){
        this.propertyName =propertyName;
        this.isASC =isASC;
    }
    public OrderCondition(){
        this.isASC=true;
    }
    /**
     * 按照属性升序排序
     * 排序列 可以是 几个列运算的结果，
     * 但要求 属性前加“_e.”，如 order by (orgId - version)，
     * 写成asc("${orgId} - ${version}");
     * @param propertyName
     * @return OrderCondition
     */
    public static OrderCondition asc(String propertyName) {
        return new OrderCondition(propertyName, true);
    }

    /**
     * 按照属性降序排序
     * 排序列 可以是 几个列运算的结果，
     * 但要求 属性前加“_e.”，如 order by (orgId - version)，
     * 写成desc("${orgId} - ${version}");
     * @param propertyName
     * @return OrderCondition
     */
    public static OrderCondition desc(String propertyName) {
        return new OrderCondition(propertyName, false);
    }

    public String getOrder() {
        return this.isASC == true ? ASC: DESC;
    }
    
    public void setOrder(String order) {
        this.isASC = (order == null || !order.equalsIgnoreCase(DESC));
    }
   
    public String getPropertyName() {
        return propertyName;
    }
    
    /**
     * 排序列 可以是 几个列运算的结果，
     * 但要求 属性前加“_e.”，如 order by (orgId - version)，
     * 写成setPropertyName("${orgId} - ${version}");
     * @param propertyName
     */
    public void setPropertyName(String propertyName) {
        this.propertyName = propertyName;
    }
    @Override
    public String toString() {
    	if (propertyName == null || propertyName.isEmpty()){
    		return "";
    	}else{
    		return propertyName + ' ' + (isASC?ASC:DESC);
    	}
    }
    public boolean isASC() {
        return isASC;
    }
    public void setASC(boolean isASC) {
        this.isASC = isASC;
    }
}
